//
//  MoreCollectionViewController.m
//  HappyBaby
//
//  Created by lanou on 15/11/28.
//  Copyright © 2015年 华哥. All rights reserved.
//

#import "MoreCollectionViewController.h"
#import "Define.h"
#import "MoreCollectionViewCell.h"
#import "ThreeModel.h"
#import "BabyPlayerViewController.h"
#import "MJRefresh.h"
@interface MoreCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong)NSMutableArray *modelArray;
@property (nonatomic, assign) NSInteger start;
@end

@implementation MoreCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (NSMutableArray *)modelArray
{
    if (!_modelArray)
    {
        _modelArray = [NSMutableArray array];
    }
    
    return _modelArray;
}


- (void)loadView
{
    //创建一个layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat width = (kScreenWidth -21)/2;
    CGFloat height = width*340/680;
    layout.itemSize = CGSizeMake(width, height);
    layout.minimumLineSpacing = 7;
    layout.minimumInteritemSpacing = 3;
    
    //创建一个collectionView；
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor orangeColor];
    //把collectionView给控制器
    self.collectionView = collectionView;
    
    //上拉加载更多
    [self.collectionView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    [self.collectionView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.start = 0;
    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    //注册cell
    [self.collectionView registerClass:[MoreCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self sessonDataWithUrl:self.url];
    
}

//解析数据
- (void)sessonDataWithUrl:(NSString *)url
{
    [[[NSURLSession sharedSession]dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSArray *array = [dic valueForKey:@"data"];
        NSMutableArray *subArray = [NSMutableArray array];
        for (NSDictionary *tempDic in array)
        {
            ThreeModel *model = [[ThreeModel alloc]init];
            [model setValuesForKeysWithDictionary:tempDic];
            [subArray addObject:model];
        }
        [self.modelArray addObject:subArray];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
        
    }]resume];
}


#pragma mark <UICollectionViewDataSource>
//返回分区数；
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{

    return self.modelArray.count;
}

//每个分区返回的item;
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *array = self.modelArray[section];

    return array.count;
}

//返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.model = self.modelArray[indexPath.section][indexPath.row];
    
    return cell;
}

//点击cell的触发方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ThreeModel *model = self.modelArray[indexPath.section][indexPath.row];
    NSString *string = [NSString stringWithFormat:@"http://shaoer.qupeiyin.com/course/detail_new?course_id=%@",model.myId];
    
    BabyPlayerViewController *babyPlayer = [[BabyPlayerViewController alloc]init];
   babyPlayer.moreUrl = string;
   [self.navigationController pushViewController:babyPlayer animated:YES];
    
}


//上 左 下 右 的间距；
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(0, 7, 0, 7);
    
}

//返回每个item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = (kScreenWidth -21)/2;
    CGFloat height = width*340/680;
    
    return CGSizeMake(width, height + 20);
    
}

//返回列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    
    return 3;
    
}

//返回行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    
    return  7;
}

//上拉加载更多；
- (void)footerRefresh
{
    self.start += 20;
    NSString *url = [NSString stringWithFormat:@"http://shaoer.qupeiyin.com/course/get_course_list?start=%@&rows=20&category_id=1&nature_id=all&level=all&sort=new&ishow=0",[NSNumber numberWithInteger:(self.start)]];
    [self sessonDataWithUrl:url];
    
    [self.collectionView.footer endRefreshing];
}

//下拉刷新；
- (void)headerRefresh
{
    [self.collectionView reloadData];
    
    [self.collectionView.header endRefreshing];
    
}



@end
